// pages/room/notice/notice.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goodsId: {
      type: Number,
      value: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
  },

  /**
   * 组件生命周期函数，在组件布局完成后执行
   */
  ready: function () {
    this.readyTimeout = setTimeout(() => {
      this.getRoomNotice();
    }, 500);
  },

  /**
   * 组件生命周期函数，在组件实例被从页面节点树移除时执行
   */
  detached: function () {
    clearTimeout(this.readyTimeout);
    clearTimeout(this.loadNextTimeout);
    clearTimeout(this.reloadTimeout);
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getRoomNotice: function () {
      app.Ajax({
        url: '/api/index.php?app=goods&act=goods_views',
        data: {
          goods_id: this.data.goodsId,
        },
        success: (res) => {
          if (res && res.length > 0) {
            this.noticeList = res;
            this.noticeIndex = 0;
            //循环显示中奖用户
            if (this.loadNextTimeout) {
              clearTimeout(this.loadNextTimeout);
            }
            this.showNotice();
            //该轮用户显示完后再次查询中奖用户列表
            this.reloadTimeout = setTimeout(() => {
              this.getRoomNotice();
            }, 6000 * this.noticeList.length)
          } else {
            this.reloadTimeout = setTimeout(() => {
              this.getRoomNotice();
            }, 6000)
          }
        }
      })
    },

    showNotice: function () {
      if (this.noticeIndex > this.noticeList.length - 1) {
        clearInterval(this.interval);
        return;
      }
      var noticeInfo = this.noticeList[this.noticeIndex];
      this.noticeIndex++;
      this.setData({
        notice: '"' + noticeInfo.user_name + '"来了',
      })

      setTimeout(() => {
        this.setData({
          notice: null,
        })
      }, 5500);

      var randomTime = Math.random() * 1000 + 5500;
      this.loadNextTimeout = setTimeout(() => {
        this.showNotice();
      }, randomTime);
    }
  }
})
